package com.shiguo365.market.display.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;


import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.R;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.display.adapter.AddressListAdapter;
import com.shiguo365.market.display.adapter.OrderListAdapter;
import com.shiguo365.market.display.base.BaseActivity;
import com.shiguo365.market.display.listener.NavigationFinishClickListener;
import com.shiguo365.market.display.util.RefreshUtils;
import com.shiguo365.market.display.util.ToastUtils;
import com.shiguo365.market.display.view.IAddressListView;
import com.shiguo365.market.presenter.contract.IAddressListPresenter;
import com.shiguo365.market.presenter.implement.AddressListPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddressListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener,IAddressListView {

    private String TAG="AddressListActivity";

    private LinearLayoutManager mLayoutManager;

    AddressListAdapter mAdapter;

    private IAddressListPresenter mAddressListPresenter;

    private List<JSONObject> jsonObjectList= new ArrayList<JSONObject>();;

    @BindView(R.id.address_list_refresh_layout)
    protected SwipeRefreshLayout refreshLayout;
    @BindView(R.id.my_recycler_view_addresslist)
    protected RecyclerView mRecyclerView;
    @BindView(R.id.address_list_toolbar)
    protected Toolbar toolbar;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_list);

        ButterKnife.bind(this); //ButterKnife初始化
        toolbar.setNavigationOnClickListener(new AddressNavigationFinishClickListener(this));

        mAddressListPresenter=new AddressListPresenter(this,this);

        Intent intent =getIntent();
        String mold=intent.getStringExtra("mold");
        if(mold == null){
            mold="null";
        }
        Log.d(TAG,mold);
        mAddressListPresenter.setMold(mold);
        mAdapter = new AddressListAdapter(mAddressListPresenter,jsonObjectList);
        mRecyclerView.setAdapter(mAdapter);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
//如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        mRecyclerView.setHasFixedSize(true);
//创建并设置Adapter
        RefreshUtils.initOnCreate(refreshLayout, this);
        RefreshUtils.refreshOnCreate(refreshLayout, this);
    }

    @Override
    public void onRefresh() {
        //添加刷新代码
        jsonObjectList.clear();

        mAddressListPresenter.getAddressListByUI(MyApplication.getUID());

    }

    @OnClick(R.id.buttonAddAddress)
    protected void mbtnAdd(){
        Intent intent =new Intent(this,AddressAddActivity.class);
        startActivityForResult(intent,1);
    }

    @Override
    public void OnGetAddressListOK(List<JSONObject> jsonObjectList) {

        for (int i = 0; i < jsonObjectList.size(); i++) {
            this.jsonObjectList.add(jsonObjectList.get(i));
        }

        mAdapter.notifyDataSetChanged();

        Log.d(TAG,jsonObjectList.toString());
    }

    @Override
    public void OnGetAddressListFinsh() {
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void delAddressByIdOK() {
        ToastUtils.with(this).show("地址删除成功！");
        RefreshUtils.refreshOnCreate(refreshLayout, this);
    }

    @Override
    public void setAddressDefaultByIdOK() {
        //ToastUtils.with(this).show("设为默认地址成功！");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

            }
        },2000);
        RefreshUtils.refreshOnCreate(refreshLayout, this);
    }

    @Override
    public void OnOrderConfirmSelectedOK() {

        Intent intent=new Intent();
        intent.putExtra("data_return",1000);
        setResult(RESULT_OK,intent);
        finish();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        RefreshUtils.refreshOnCreate(refreshLayout, this);
    }


    class AddressNavigationFinishClickListener implements View.OnClickListener {

        private final Activity activity;

        public AddressNavigationFinishClickListener(@NonNull Activity activity) {
            this.activity = activity;
        }

        @Override
        public void onClick(View v) {
            Intent intent=new Intent();
            intent.putExtra("data_return",1000);
            setResult(RESULT_OK,intent);
            Log.d("A","fanhui");
            ActivityCompat.finishAfterTransition(activity);
        }

    }
}
